package com.example.contentprovider;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private EditText name;
    private EditText phone;
    private Button add,sel;
    private ListView display;
    private TextView display1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (EditText) findViewById(R.id.name);
        phone = (EditText) findViewById(R.id.phone);
        display = (ListView) findViewById(R.id.display);
        add = (Button) findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
                intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
                intent.putExtra(ContactsContract.Intents.Insert.NAME,name.getText().toString());
                intent.putExtra(ContactsContract.Intents.Insert.PHONE,phone.getText().toString());
                startActivity(intent);
            }
        });
        sel = (Button) findViewById(R.id.sel);
        sel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                ContentResolver contentResolver = MainActivity.this.getContentResolver();
//                Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
//                        null,null,null,null);
//                SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(ContactManageActivity.this,
//                        R.layout.contact_item,
//                        cursor,
//                        new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME},
//                        new int[]{R.id.contact_text,R.id.num_text}
//                        );
//                contactlist.setAdapter(simpleCursorAdapter);
//                SimpleAdapter adapter = new SimpleAdapter(
//                        MainActivity.this,R.layout.list_item.,cursor,new String[]{ContactsContract.Contacts._ID},
//                ContactsContract.Contacts.DISPLAY_NAME},new int[]{R.id.txtId,R.id.txtName},0
//                );
//                SimpleCursorAdapter adapter = new SimpleCursorAdapter(
//                        MainActivity.this,
//                        R.layout.list_item,
//                        cursor,
//                        new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME},
//                        new int[] {R.id.txtId,R.id.txtName}
//                );
//                display.setAdapter(adapter);
               // ArrayList<Map<String, Object>> sb = new ArrayList<Map<String, Object>>();
//                if(cursor!=null) {
//                    while (cursor.moveToNext()) {
//                        //Map<String, Object> listem = new HashMap<String, Object>();
//                        int inIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
//                        Log.i("info", "1");
//                        int id = cursor.getInt(inIndex);
//                        Log.i("info", "2");
//                        int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
//                        Log.i("info", "3");
//                        String name = cursor.getString(nameIndex);
//                        Log.i("info", "4");
//                        Cursor phone1 = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
//                                new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null);
//                        Log.i("info", "5");
//                        while (phone1.moveToNext()) {
//                            int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
//                            String phoneNumber = cursor.getString(phoneIndex);
//                            Log.i("info", "name" +phoneNumber);
//                        }
//
////                        int inIndexPhone = phone1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
////                        String PhoneNumber = phone1.getString(inIndexPhone);
//                        Log.i("info", "name" + name);
////                         listem.put("name", name);
////                         listem.put("phone", PhoneNumber);
////                         sb.add(listem);
//                    }
//                }
//                SimpleAdapter simplead = new SimpleAdapter(MainActivity.this, sb,
//                        R.layout.list_item, new String[] { "name", "phone" },
//                        new int[] {R.id.txtName,R.id.txtPhone});
//                        display.setAdapter(simplead);
                ContentResolver cr = getContentResolver();
                Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI, new String[] { ContactsContract.Contacts._ID,
                        ContactsContract.Contacts.DISPLAY_NAME }, null, null, null);
                ArrayList<Map<String, Object>> sb = new ArrayList<Map<String, Object>>();
                if (c != null) {
                    while (c.moveToNext()) {
                        Map<String, Object> listem = new HashMap<String, Object>();
                        int id = c.getInt(c.getColumnIndex("_id"));
                        String Name = c.getString(c.getColumnIndex("display_name"));
                        Log.i("info", "_id:" + id);
                        Log.i("info",
                                "name:" + c.getString(c.getColumnIndex("display_name")));
                        Cursor c1 = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {
                                        ContactsContract.CommonDataKinds.Phone.NUMBER,},
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null);
                        // 根据联系人ID查询出联系人的电话号码
                        if (c1 != null) {
                            while (c1.moveToNext()) {
                                String PhoneNumber = c1.getString(c1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    Log.i("info",
                                            "手机："
                                                    + c1.getString(c1
                                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                                listem.put("name", Name);
                                listem.put("phone", PhoneNumber);

                            }
                            c1.close();
                            Log.i("info", "name" + name);

                            sb.add(listem);
                        }

                    }
                    c.close();
                    SimpleAdapter simplead = new SimpleAdapter(MainActivity.this, sb,
                        R.layout.list_item, new String[] { "name", "phone" },
                        new int[] {R.id.txtName,R.id.txtPhone});
                        display.setAdapter(simplead);
                }
            }
        });
    }
}
